home *** CD-ROM | disk | FTP | other *** search
- Path: news.itd.umich.edu!clahey
- From: clahey@rep00703.reshall.umich.edu (Chris Lahey)
- Newsgroups: comp.lang.c
- Subject: Re: Nested Structures in C - A Question
- Date: 28 Jan 1996 20:03:35 GMT
- Organization: University of Michigan
- Message-ID: <CLAHEY.96Jan28200335@rep00703.reshall.umich.edu>
- References: <36400002@peg>
- NNTP-Posting-Host: rep00703.reshall.umich.edu
- In-reply-to: tmccoy@peg.apc.org's message of 29 Jan 96 01:38 GMT+1000
-
- I believe that you are looking at this wrong. Remember this.
- When you define a structure you say what data is going to go
- in it. You give all of this data names. A struct is just
- like any other data type (For what we're discussing.) What
- you are trying to do is almost equivelent to:
-
- struct outer {
- int var1;
- int var2;
- int var3;
- double;
- };
-
- It's not quite the same. Yours will compile. The struct inner
- is just useless. You've defined a type and then ended your block.
- What you probably want is either
-
- struct outer {
- int var1;
- int var2;
- int var3;
- struct {
- int nested1;
- int nested2;
- } inner;
- };
-
- which will allow you to access the inner structure as you wanted,
- or
-
- struct inner {
- int nested1;
- int nested2;
- };
-
- struct outer {
- int var1;
- int var2;
- int var3;
- struct inner var4;
- };
-
- which will not only allow you to access the inner structure as
- instance.var4 but also to create instances of the inner structure
- as normal variables. In either case no memory is allocated until
- you create an instance of the struct. A structure allocating
- memory when it sees struct inner {...} DUMMY; is very similar to
- it allocation memory when it sees int var1;. Both are instances
- of a type (DUMMY is an instance of struct inner and var1 is an
- instance of int) and no memory should be allocated until an
- instance of the struct is declared outside a structure body.
- I hope I was helpful. Good luck.
- Chris
-